package com.winchain.chainbill.bill.biz.send.service.impl;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.ChainCallback;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.base.service.CbSndService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillRcv4SndCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillSndCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillSndMsgCmd;
import com.winchain.chainbill.bill.biz.send.vo.BillSnd;
import com.winchain.chainbill.chain.callback.CallbackService;
import com.winchain.chainbill.trade.cmd.TranCallbackCmd;
import com.winchain.chainbill.trade.cmd.TranRollbackCmd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("billSndCallbackService")
public class BillSndCallbackServiceImpl implements CallbackService {
	@Autowired
	private CbSndService cbSndService;
	@Autowired
	private BillOptCmd<CbSnd> billOptCmd;
	@Autowired
	private BillSndCmd billSndCmd;
	@Autowired
	private BillMoveCmd<CbSnd> billMoveCmd;
	@Autowired
	private BillRcv4SndCmd billRcv4SndCmd;
	@Autowired
	private BillSndMsgCmd billSndMsgCmd;
	@Autowired
	private TranCallbackCmd<Bill<CbSnd>> tranCallbackCmd;
	@Autowired
	private TranRollbackCmd<Bill<CbSnd>> tranRollbackCmd;

	public String getType() {
		return "cb_snd";
	}

	public void confirm(AcctTxRec tran) {
		String billId = tran.getBizId();
		BillSnd record = this.cbSndService.getBillSndById(billId);
		Bill<CbSnd> bill = new Bill();
		bill.setRec(record.getRec());
		bill.setSource(record.getSnd());

		CbOpt opt = new CbOpt();
		opt.setCurState("S024");
		if (presendFlag(record)) {
			opt.setOptCode("EX16");
		} else {
			opt.setOptCode("EX11");
		}
		opt.setRemark("����������������������������");

		CallbackContext<Bill<CbSnd>, AcctTxRec> context = new CallbackContext();
		context.setSource(bill);
		context.setTarget(tran);
		context.setValue("Operation", opt);

		context.setValue("bizType", getType());

		ChainCallback<Bill<CbSnd>, AcctTxRec> ccb = new ChainCallback();
		ccb.add(this.billOptCmd);
		ccb.add(this.billSndCmd);
		ccb.add(this.billMoveCmd);
		ccb.add(this.billRcv4SndCmd);
		ccb.add(this.tranCallbackCmd);

		ccb.add(this.billSndMsgCmd);
		ccb.callback(context);
	}

	public void cancel(AcctTxRec tran) {
		String billId = tran.getBizId();
		BillSnd record = this.cbSndService.getBillSndById(billId);
		Bill<CbSnd> bill = new Bill();
		bill.setRec(record.getRec());
		bill.setSource(record.getSnd());

		CbOpt opt = new CbOpt();
		opt.setCurState("S024");
		opt.setOptCode("EX12");

		opt.setRemark("����������������������������");

		CallbackContext<Bill<CbSnd>, AcctTxRec> context = new CallbackContext();
		context.setSource(bill);
		context.setTarget(tran);
		context.setValue("Operation", opt);

		ChainCallback<Bill<CbSnd>, AcctTxRec> ccb = new ChainCallback();
		ccb.add(this.billOptCmd);
		ccb.add(this.billSndCmd);
		ccb.add(this.billMoveCmd);

		ccb.add(this.tranRollbackCmd);
		ccb.callback(context);
	}

	private boolean presendFlag(BillSnd record) {
		boolean presendFlag = false;
		String sndDate = record.getSndDate();

		String revDate = DateUtil.getSystemDate();
		if (SysDateUtil.compare(sndDate, revDate) > 0) {
			presendFlag = true;
		}
		return presendFlag;
	}
}
